/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.dusa.worklog.viewer;

import cz.dusa.worklog.controller.WorklogDataException;
import cz.dusa.worklog.controller.WorklogDataListener;
import cz.dusa.worklog.controller.WorklogDataManager;
import cz.dusa.worklog.datalib.Customer;
import cz.dusa.worklog.datalib.Project;
import cz.dusa.worklog.datalib.WorklogData;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.Exceptions;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;

/**
 *
 * @author vdusa
 */
public class CustomerNode extends AbstractNode implements WorklogDataListener<WorklogData> {
    private Customer customer;
    static final String ICON_PATH = "cz/dusa/worklog/viewer/Contact.png";
    ProjectChildFactory childFactory = null;
    
    CustomerNode(Customer c) {
        super(Children.LEAF);

        WorklogDataManager.getInstance().addWorklogDataListener((WorklogDataListener) this, Project.class);
        WorklogDataManager.getInstance().addWorklogDataListener((WorklogDataListener) this, Customer.class, c.getId());
        childFactory = new ProjectChildFactory(c.getProjects());
        setChildren(Children.create(childFactory, false));
        
        setCustomer(c);
        setIconBaseWithExtension(ICON_PATH);
    }

    @Override
    public Image getIcon(int type) {
        return ImageUtilities.loadImage(ICON_PATH);
    }

    @Override
    public Image getOpenedIcon(int type) {
        return getIcon(type);
    }

    @Override
    public String getDisplayName() {
        return getName();
    }

    @Override
    public Action[] getActions(boolean context) {
        Action[] result = new Action[]{
            new AddProjectAction(),
            new OpenAction(),
            new DeleteAction()
        };
        return result;
    }

    @Override
    public Action getPreferredAction() {
        return new OpenAction();
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
        setName(customer.getName());
        setShortDescription(customer.getName() + ", " + customer.getCity());
    }

    public void afterCreate(WorklogData wd) {
        if(wd instanceof Project){
            Project project = (Project)wd;
            if (project.getCustomer().getId().equals(getCustomer().getId())){
                childFactory.addProject(project);
            }
        }
    }

    public void afterUpdate(WorklogData wd) {
        if(wd instanceof Project){
            Project project = (Project)wd;
            if (project.getCustomer().getId().equals(getCustomer().getId())){
                childFactory.updateProject(project);
            }
        }

        if(wd instanceof Customer){
            setCustomer((Customer)wd);
        }

    }

    public void beforeDelete(WorklogData wd) {
        if(wd instanceof Project)
            childFactory.removeProject((Project)wd);
    }

    private final class DeleteAction extends AbstractAction {

        public DeleteAction() {
            putValue(Action.NAME, NbBundle.getMessage(CustomersTopComponent.class, "Delete"));
        }

        public void actionPerformed(ActionEvent e) {
            if(JOptionPane.showConfirmDialog(null, NbBundle.getMessage(CustomersTopComponent.class, "DeleteCustomerConfirmDialogText"), NbBundle.getMessage(CustomersTopComponent.class, "DeleteCustomerConfirmDialogTitle"), JOptionPane.YES_NO_OPTION)!=JOptionPane.YES_OPTION)
                return;

            try {            
                WorklogWindowManager.getInstance().closeWindow(customer);
                WorklogDataManager.getInstance().remove(customer);
            } catch (WorklogDataException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    private final class OpenAction extends AbstractAction{
        public OpenAction() {
            putValue(Action.NAME, NbBundle.getMessage(CustomersTopComponent.class, "Open"));
        }

        public void actionPerformed(ActionEvent e) {
            WorklogWindowManager.getInstance().openWindow(customer);
        }
    }

    private final class AddProjectAction extends AbstractAction{
        public AddProjectAction(){
            putValue(Action.NAME, NbBundle.getMessage(CustomersTopComponent.class, "AddProject"));
        }

        public void actionPerformed(ActionEvent e) {
            Project project = WorklogDataManager.getInstance().getNewProject(customer);
            WorklogWindowManager.getInstance().openWindow(project);
        }
    }
}
